Skip to content

crypto: the library sets arm64's DIT around every cryptographic operation, and the lanes' defects are fixed at their cause - #11

Merged
pinhopro merged 22 commits into
mainfrom
crypto/dit
Nov 21, 2026
Merged

pinhopro merged 22 commits into
mainfrom
crypto/dit

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

The change data-independent-timing (proposals, libraries/crypto/openspec/changes/data-independent-timing): crypto sets arm64's DIT itself around every public cryptographic operation, on every backend, and restores the thread's state.

  • detail::timing_guard sets PSTATE.DIT for its scope on arm64 macOS, iOS, Linux and Android where the CPU has FEAT_DIT, and restores what it changed; elsewhere it is empty.
  • Every public operation of the ciphers, the boxes, PEM, Ed25519, X25519, kx, their formats, secp256k1 and its groups holds the guard as its first statement (110 guards).
  • A test seam, WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED(where), exists only where a test defines it; a coverage test per backend proves each function enters its own guard first. A release program keeps no guarded function's name or header path (timing_unseamed_release).
  • The constant-time lane no longer sets DIT: it measures what a program gets from the library. dudect_planted still finds its leak inside the guard.
  • bench/timing.cpp and collect.py --timing publish the guard's cost. The figures were measured on a loaded machine and are marked provisional; a final quiet measurement pass follows after the streams of 2026-11-20 merge (design "As built", open item).

Defects the local lanes found, fixed here at their cause (each also on main):

  • A memory resource fails by throwing, never by returning null: allocate is declared never to return null, so release builds removed the OpenSSL adapter's null check and wrote through null with exceptions off.
  • The OpenSSL backend's resets zero their context pointers with wipe_object; GCC 15 at -O3 dropped the plain null stores in destructors.
  • Two GCC 15 -O3 diagnostics in tests (mismatched-new-delete, stringop-overflow), and Pyright's optional subscripts in collect.py.

Verified locally (CI deferred by the owner): native clang and gcc-15, wasip2, wasip3 and Emscripten, each debug and release, from scratch, every test and example passing; crypto's tests on arm64 Linux with DIT; the Linux lint (clean) and the macOS lint; MSVC 14.3 and 14.5 under Wine (no failure beyond the known Wine ones); the superproject's tools tests.

R added 22 commits November 20, 2026 19:51
…res the thread's state, with a seam a test counts
…ate and full_public_key alone in their checks, and compares after
…ers its own timing guard

timing_guard takes the std::source_location of its declaration, and the seam
WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED(where) passes it on, empty in a program's
build. The test seam records the function of the first guard a call enters,
and require_enters and covered fail, naming the check, when no guard fired or
when that first guard is not the one of the function the check names, read in
the spellings of clang, GCC and MSVC with its qualifying scope, so that a
forwarder or a function calling another guarded one cannot pass without its
own guard. covered_around_callee goes; each coverage file lists the families
and functions it covers; BearSSL and OpenSSL also cover AES-192-GCM.
… its outermost parameter list, and refuses a check that names no scope
…DIT set or a guard leaves it wrong, and reports each part's fastest and slowest run
…art's spread and UTC dates, explains missing data, and marks the figures measured under load as provisional
… lane without DIT, and the changelog says where webcpp sets DIT
…he seam, so that no guarded function's name or header path reaches a release program on arm64
…h on its own, and refuses malformed data and program lines with a message
… which allocate is declared never to do, so the adapter no longer checks a null that release builds removed

libc++ and libstdc++ declare std::pmr::memory_resource::allocate returns_nonnull, so the adapter's null check after it was folded away and a null from a test resource was written through (SIGSEGV in clang release, exceptions off). The contract now matches the standard: a resource that cannot allocate throws, and without exceptions ends the process, as std::pmr's own resources do. The failing-resource tests run with exceptions only; adapter's throwing part likewise.
…ect, since GCC 15 at -O3 drops a plain null store in a destructor, and the wipe test reads destroyed storage through a pointer the optimiser cannot follow

GCC 15 release builds failed wipe_objects_openssl with -Werror=uninitialized: the test read
the bytes of a destroyed object, which GCC takes for indeterminate after the end-of-lifetime
clobber of the destructor. With the read made opaque (the storage's address read back from a
volatile), the test failed at run time on gcm and chacha20_poly1305: GCC had dropped the
std::exchange(context, nullptr) of their resets as a dead store, so the destroyed object still
held the freed context's address. The resets now free the contexts and then zero the state
with wipe_object, as the other backends do; the test passes, and fails again without the fix.
…h a volatile, as counting_test does, so that GCC 15 at -O3 does not pair the inlined replaced delete's free with operator new
…and the IV once and changes each byte back after its check, which GCC 15 at -O3 no longer reports as an overflowing copy

A vector copy inside each iteration made GCC 15 -O3 without exceptions report a memcpy of
2^63 bytes or more in libstdc++'s uninitialized_copy (-Wstringop-overflow) in
chacha_openssl_no_exceptions: a false positive, since the sizes come from short hex literals.
…dict, so Pyright sees that the timing figures are read from an object, never from None
…y any optimiser, and collect.py's message names a line that is not an object
@pinhopro
pinhopro merged commit 3f27988 into main Nov 21, 2026
1 of 8 checks passed
@pinhopro
pinhopro deleted the crypto/dit branch November 21, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant